package com.zhxin.nettylab.nio.chapter4;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.util.Iterator;

/**
 * @ClassName MyServer
 * @Description //TODO
 * @Author singleZhang
 * @Email 405780096@qq.com
 * @Date 2021/2/23 0023 下午 4:55
 **/
public class MyServer {

    private static Selector selector;

    public static void main(String[] args) {
        MyServer server = new MyServer();
    }

    public void startServer() throws IOException {
        selector = Selector.open();

        ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
        ServerSocket serverSocket = serverSocketChannel.socket();
        serverSocket.bind(new InetSocketAddress(8899));

        serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);

        while (selector.select() > 0){
            Iterator<SelectionKey> keys = selector.selectedKeys().iterator();

            while (keys.hasNext()){
                SelectionKey key = keys.next();

                if(key.isAcceptable()){

                }else if(key.isReadable()){

                }
                //移除选择键
                keys.remove();
            }
        }
    }
}
